<?php

/**
 * 默认控制器
 */
class Controller_Asp extends Controller_Abstract
{
	private $leftchar = '';
	private $rightchar = '';
	function _response($data)
	{
		exit($this->leftchar.$data.$this->rightchar);
	}
	function deString($string)
	{
		$param = explode('&', $string);
		$result = array();
		foreach($param as $k=>$v)
		{
			$split = explode('=', $v);
			$result[strtolower($split[0])] = $split[1];
		}
		return $result;
	}
	function actioncall()
	{
		$arr = $this->deString($_SERVER['QUERY_STRING']);
		Helper_Common::log(print_r($_SERVER,true),'aspcall');
		if(isset($arr['userid']) && isset($arr['called']))
		{
			#call
			$mobile = $arr['userid'];
			$phone = $arr['called'];
			if(!Helper_Param::_phone($mobile))
			{
				$result = '0'; //手机号码格式不正确
			}else{
				$user = Acctuser::checkAcctname($mobile);
				if (!$user->id()){
					$result = '0'; //不存在此绑定号码
				}else if($user->callpwd == 1)
				{
					$result = '0';//设置为加密呼叫了	
				}else{
					$isone = Cbsubmit::find('bindname = ? and destination_number = ?',$mobile,$phone)->getCount('id');
					if($isone){
						$result = '0';//存在此呼叫队列 请勿重复提交
					}else{
						//if(!Helper_Param::_phone($phone))
						//{
						//	$result = '0';//被叫号码格式不正确
						//}else{
							Cbsubmit::call($mobile, $phone);
							$result = '1';//提交成功
						//}
							
					}
				}
			}
			$this->_response($result);
		}else if (isset($arr['userid']) && ! isset($arr['called']) && !isset($arr['cardid']))
		{
			#balance
			$mobile = $arr['userid'];
			if (!Helper_Param::_phone($mobile))
			{
				$result = sprintf('%f',0);
			}else
			{
				$user = Acctuser::checkAcctname($mobile);
				//$bind = Userbindtel::find('bindtel = ?',$mobile)->setColumns('id,acctname')->getOne();
				if(!$user->id())
				{
					$result = sprintf('%f',0);
				}else{
					$result = sprintf('%f',isset($user->balance)?$user->balance:0);
				}
			}
			$this->_response($result);
		}else if (isset($arr['cardid']) && isset($arr['cardpwd']))
		{
			#pay
			$mobile = $arr['userid'];
			$cardid = $arr['cardid'];
			$cardpwd = $arr['cardpwd'];
			if(!Helper_Param::_call($mobile))
			{
				$result = '0'; //手机号码格式不正确
			}else{
				//$user = Acctuser::checkAcctname($mobile);
				//if(!$user->id())
				//{
				//	$result = 'ERR2'; //不存在此绑定号码
				//}else{
				$flag = Acctuser::use_card($mobile, $cardid, $cardpwd);
				if($flag === true)
				{
					$card = Vouchercard::find('cardname = ? and password = ?',$cardid,$cardpwd)->setColumns('id,balance')->getOne();
					$result = sprintf('%f',$card->balance);
				}else{
					$result = '0';
				}
				//}
			}
			$this->_response($result);
		}
	}
}